home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MYMUD21.ZIP / MMUD21.ZIP / SOURCE / SOURCE.ZIP / BIN_DB.PAS next >
Pascal/Delphi Source File  |  1995-01-21  |  16KB  |  535 lines

  1. {$I COPYRGHT.INC}
  2.  
  3. (*----------------------------------------------------------------------------*
  4.  
  5.    Binary database routines. Implements a binary database for MyMUD. The
  6.    database itself is modelled after the tinymud database.
  7.  
  8.  *---------------------------------------------------------------------------*)
  9.  
  10. Unit BIN_DB;
  11. interface
  12. Uses Dos,Header,MyIO,Out_Proc;
  13.  
  14. Type Database = Object
  15.                   ObjRec  : ObjRecord;         { Hold the current objectrecord }
  16.                   TxtRec  : TextRecord;        { Hold the current text         }
  17.  
  18.                   ObjFile : File of ObjRecord;
  19.                   TxtFile : File;
  20.  
  21.                   DBName  : ComStr;         { The name of the current database }
  22.                   CObjNr  : Integer;        { The last read objectrecord       }
  23.  
  24.                   { The player functions. Search and modify the .PLY file      }
  25.  
  26.                   Function FindPlayer(UserName : NameString):Integer;
  27.                   Procedure AddPlayer(ObjNr : Integer);
  28.  
  29.                   { The Database functions. Search and modify the .IDX file    }
  30.  
  31.                   Procedure Init;
  32.                   Procedure ReadObj(Nr : Integer);
  33.                   Function  ExistObj(Nr : Integer):Boolean;
  34.                   Procedure UpdateObj(Nr : Integer);
  35.                   Function AddObj:integer;
  36.                   Procedure WriteRecord;
  37.                   Procedure Final;
  38.                   Procedure ResetAll;
  39.  
  40.                   { The description file functions. Search and modify the.TXT  }
  41.                   { file                                                       }
  42.  
  43.                   Procedure Describe(Msg : String);
  44.                   Procedure Finger(Msg : String);
  45.                   Function  Macro:String;
  46.                   Procedure OFail(Msg : String);
  47.                   Procedure OSuccess(Msg : String);
  48.                   Procedure Fail(Msg : String);
  49.                   Procedure Success(Msg : String);
  50.                   Function Name:String;
  51.  
  52.                   { the flag functions.                                       }
  53.  
  54.                   Function IsRoom:Boolean;
  55.                   Function IsThing:Boolean;
  56.                   Function IsExit:Boolean;
  57.                   Function IsPlayer:Boolean;
  58.                   Function IsDrone:Boolean;
  59.  
  60.                   Function LevelOk(Level : Byte):Boolean;
  61.  
  62.                   Function IsTemple:Boolean;
  63.                   Function IsHaven:Boolean;
  64.                   Function IsShop:Boolean;
  65.                   Function IsLoud:Boolean;
  66.  
  67.                   Function CanTeleport:Boolean;
  68.  
  69.                   Function IsLinkOk:Boolean;
  70.                   Function IsSticky:Boolean;
  71.                   Function IsInvisible:Boolean;
  72.                   Function IsForSale:Boolean;
  73.                   Function IsChownOK:Boolean;
  74.  
  75.                   Function IsOwnedBy(Player : Integer):Boolean;
  76.                   Function IsOwner(ObjNr : Integer):Boolean;
  77.  
  78.                   Function WhichGender:GenderType;
  79.  
  80.  
  81.  
  82.                End;
  83.  
  84. Type ContextType = Record
  85.                     Player     : Integer;
  86.                     Room       : Integer;
  87.                     PlayerName : String[40];
  88.                     Level      : Byte;
  89.                     Gender     : GenderType;
  90.                     Note       : String[50];
  91.                     DB         : Database;
  92.                    End;
  93.  
  94.  
  95. Function MaxLen(Len : Word):Word;
  96.  
  97. Implementation
  98. Uses Misc;
  99.  
  100. Function MaxLen(Len : Word):Word;
  101. Begin
  102. If Len>Header.DescMax
  103.    Then MaxLen:=Header.DescMax
  104.    Else MaxLen:=Len;
  105. End;
  106.  
  107.  
  108. (*---------------------------------------------------------------------------*
  109.    Converts a string to all uppercase
  110.  *---------------------------------------------------------------------------*)
  111. Function UpStr(S : String):String;
  112. Var C : Byte;
  113. Begin
  114. For C:=1 To Length(S) Do
  115.  S[C]:=Upcase(S[C]);
  116. UpStr:=S;
  117. End;
  118.  
  119. (*---------------------------------------------------------------------------*
  120.    Find a player in the database
  121.  *---------------------------------------------------------------------------*)
  122. Function Database.FindPlayer(UserName : NameString):Integer;
  123. Var Ply : File of Integer;
  124.     Rec : Integer;
  125. Begin
  126. ResetAll;
  127. FileMode:=ReadWrite+ShareDenyNone;
  128. Assign(PLY,DBName+'.PLY');
  129. Reset(PLY);
  130. While (Not Eof(Ply)) and (UpStr(Name)<>UpStr(UserName)) Do
  131.  Begin
  132.  Read(Ply,Rec);
  133.  ReadObj(Rec);
  134.  End;
  135. Close(Ply);
  136. If UpStr(Name)<>UpStr(UserName)
  137.    Then FindPlayer:=NOTHING
  138.    Else FindPlayer:=Rec;
  139. End;
  140.  
  141. (*---------------------------------------------------------------------------*
  142.    Add a new user to the .PLY file.
  143.  *---------------------------------------------------------------------------*)
  144. Procedure Database.AddPlayer(ObjNr : Integer);
  145. Var Ply : File of Integer;
  146. Begin
  147. FileMode:=ReadWrite+ShareDenyNone;
  148. Assign(PLY,DBName+'.PLY');
  149. Reset(PLY);
  150. Seek(PLY,FileSize(PLY));
  151. Write(PLY,ObjNr);
  152. Close(Ply);
  153. If IoResult<>0
  154.    Then Halt(1);
  155. End;
  156.  
  157. (*---------------------------------------------------------------------------*
  158.    Initialize the database functions. Always call first!
  159.  *---------------------------------------------------------------------------*)
  160. Procedure Database.Init;
  161. Begin
  162. DBName:=ParamStr(1);
  163. If Pos('.',DBName)>0
  164.    Then DBName:=Copy(DBName,1,Pos('.',DBName)-1);
  165.  
  166. FileMode:=ReadWrite+ShareDenyNone;
  167. Assign(OBJFile,DBName+'.IDX');
  168. Reset(OBJFile);
  169. Assign(TXTFile,DBName+'.DAT');
  170. Reset(TXTFile,1);
  171.  
  172. FillChar(ObjRec,SizeOf(ObjRec),#00);
  173. FillChar(TxtRec,SizeOf(TxtRec),#00);
  174. CObjNr :=NOTHING;
  175. End;
  176.  
  177. (*---------------------------------------------------------------------------*
  178.    Read a record from the file
  179.  *---------------------------------------------------------------------------*)
  180. Procedure DataBase.ReadObj(Nr : Integer);
  181. Begin
  182. If (Nr=CObjNr)
  183.    Then Exit
  184.    Else CObjNr:=Nr;
  185. Seek(ObjFile,Nr);
  186. Read(ObjFile,ObjRec);
  187. If IoResult<>0
  188.    Then Halt(2);
  189. End;
  190.  
  191. Function DataBase.ExistObj(Nr : Integer):Boolean;
  192. Var Old : LongInt;
  193.     Tmp : LongInt;
  194. Begin
  195. Old:=FilePos(ObjFile);
  196. Tmp:=FileSize(ObjFile);
  197. ExistObj:=Tmp>=Nr;
  198. Seek(ObjFile,Old);
  199. End;
  200.  
  201. Procedure Database.UpdateObj(Nr : Integer);
  202. Begin
  203. Seek(ObjFile,Nr);
  204. Write(ObjFile,ObjRec);
  205. If IoResult<>0
  206.    Then Begin
  207.         My_WriteLn('ObjRec nr. '+Nr2Str(Nr));
  208.         RunError(2);
  209.         End;
  210. CObjNr:=NOTHING;
  211. End;
  212.  
  213.  
  214. (*---------------------------------------------------------------------------*
  215.    Reset the database records.
  216.  *---------------------------------------------------------------------------*)
  217. Procedure DataBase.ResetAll;
  218. Begin
  219. FillChar(ObjRec,SizeOf(ObjRec),#00);
  220. FillChar(TxtRec,SizeOf(TxtRec),#00);
  221. CObjNr :=NOTHING;
  222. End;
  223.  
  224. (*---------------------------------------------------------------------------*
  225.    Close the databasefiles.
  226.  *---------------------------------------------------------------------------*)
  227. Procedure Database.Final;
  228. Begin
  229. Close(TxtFile);
  230. Close(ObjFile);
  231. End;
  232.  
  233.  
  234. (*---------------------------------------------------------------------------*
  235.   Add an object to the database
  236.  *---------------------------------------------------------------------------*)
  237. Function DataBase.AddObj:Integer;
  238. VAR NewNr:Integer;
  239. Begin
  240. NewNr:=FileSize(ObjFile);
  241. Seek(ObjFile, NewNr);
  242. Write(ObjFile,ObjRec);
  243. AddObj:=NewNr;
  244. End;
  245.  
  246. (*---------------------------------------------------------------------------*
  247.   Write the contents of the current record. (Debugging!)
  248.  *---------------------------------------------------------------------------*)
  249. Procedure Database.WriteRecord;
  250. Begin
  251. With ObjRec Do
  252.  Begin
  253.  My_WriteLn('=================[Record]==========================');
  254.  My_WriteLn('ObjNr    : '+Nr2Str(CObjNr));
  255.  My_WriteLn('Name     : '+Name);
  256.  My_WriteLn('Password : '+Password);
  257.  My_WriteLn('Key      : '+Key);
  258.  My_WriteLn('Location : '+Nr2Str(Location));
  259.  My_WriteLn('Contents : '+Nr2Str(Contents));
  260.  My_WriteLn('Exits    : '+Nr2Str(Exits));
  261.  My_WriteLn('Next     : '+Nr2Str(Next));
  262.  My_WriteLn('Owner    : '+Nr2Str(Owner));
  263.  My_WriteLn('Pennies  : '+Nr2Str(Pennies));
  264.  My_WriteLn('Type     : '+Nr2Str(ObjType));
  265.  My_WriteLn('Level    : '+Nr2Str(ObjLevel));
  266.  My_WriteLn('Garbage  : '+Nr2Str(Garbage));
  267.  My_WriteLn('Sex      : '+Nr2Str(Sex));
  268.  My_WriteLn('GFlags   : '+Nr2Str(GenFlags));
  269.  My_WriteLn('AFlags   : '+Nr2Str(Attr_Flags));
  270.  My_WriteLn('RFlags   : '+Nr2Str(Room_Flags));
  271.  My_WriteLn('');
  272.  End;
  273. End;
  274.  
  275. (*---------------------------------------------------------------------------*
  276.   Write the description of the current object
  277.  *---------------------------------------------------------------------------*)
  278. Procedure Database.Describe(Msg : String);
  279. Var RR : Word;
  280.     Cnt: Word;
  281.     Len: Word;
  282. Begin
  283. FillChar(TxtRec,SizeOf(TxtRec),#00);
  284. If ObjRec.Desc.Length<>0
  285.    Then Begin
  286.         Seek(TxtFile,ObjRec.Desc.Start);
  287.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.Desc.Length),RR);
  288.         End
  289.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  290. If TxtRec[0]=#00
  291.    Then My_WriteLn('You don''t see anything special.')
  292.    Else WriteText(TxtRec);
  293. End;
  294.  
  295. (*---------------------------------------------------------------------------*
  296.   Write the fingerinfo of the current object
  297.  *---------------------------------------------------------------------------*)
  298. Procedure Database.Finger(Msg : String);
  299. Var RR : Word;
  300.     Cnt: Word;
  301.     Len: Word;
  302. Begin
  303. FillChar(TxtRec,SizeOf(TxtRec),#00);
  304. If ObjRec.Finger.Length<>0
  305.    Then Begin
  306.         Seek(TxtFile,ObjRec.Finger.Start);
  307.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.Finger.Length),RR);
  308.         End
  309.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  310. If TxtRec[0]=#00
  311.    Then My_WriteLn('You don''t see anything special.')
  312.    Else WriteText(TxtRec);
  313. End;
  314.  
  315. (*---------------------------------------------------------------------------*
  316.    Return a macro string
  317.  *---------------------------------------------------------------------------*)
  318. Function Database.Macro:String;
  319. Var RR : Word;
  320.     Cnt: Word;
  321.     S  : String;
  322. Begin
  323. FillChar(TxtRec,SizeOf(TxtRec),#00);
  324. Seek(TxtFile,ObjRec.Macro.Start);
  325. BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.Macro.Length),RR);
  326. Cnt:=0;
  327. S:='';
  328. While (Cnt<=RR) and (Length(S)<255) Do
  329.  Begin
  330.  Case TxtRec[Cnt] of
  331.   #00 : ;
  332.   #13 : Begin
  333.         If TxtRec[Cnt+1]=#10
  334.            then Inc(Cnt);
  335.         S:=S+'^';
  336.         End;
  337.   #10 : Begin
  338.         If TxtRec[Cnt+1]=#13
  339.            then Inc(Cnt);
  340.         S:=S+'^';
  341.         End;
  342.   #9  : S:=S+' ';
  343.   #8  : ;
  344.   Else S:=S+TxtRec[Cnt];
  345.  End;
  346.  Inc(Cnt);
  347.  End;
  348. Macro:=S;
  349. End;
  350.  
  351. (*---------------------------------------------------------------------------*
  352.   Write the FAIL tekst of the current record
  353.  *---------------------------------------------------------------------------*)
  354. Procedure Database.Fail(Msg : String);
  355. Var RR : Word;
  356.     Cnt: Word;
  357.     Len: Word;
  358. Begin
  359. FillChar(TxtRec,SizeOf(TxtRec),#00);
  360. If ObjRec.Fail.Length<>0
  361.    Then Begin
  362.         Seek(TxtFile,ObjRec.Fail.Start);
  363.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.Fail.Length),RR);
  364.         End
  365.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  366. End;
  367.  
  368. (*---------------------------------------------------------------------------*
  369.   Write the SUCCESS tekst of the current record
  370.  *---------------------------------------------------------------------------*)
  371. Procedure Database.Success(Msg : String);
  372. Var RR : Word;
  373.     Cnt: Word;
  374.     Len: Word;
  375. Begin
  376. FillChar(TxtRec,SizeOf(TxtRec),#00);
  377. If ObjRec.Success.Length<>0
  378.    Then Begin
  379.         Seek(TxtFile,ObjRec.Success.Start);
  380.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.Success.Length),RR);
  381.         End
  382.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  383. End;
  384.  
  385. (*---------------------------------------------------------------------------*
  386.   Read the OFAIL tekst of the current record
  387.  *---------------------------------------------------------------------------*)
  388.  
  389. Procedure Database.OFail(Msg : String);
  390. Var RR : Word;
  391.     Cnt: Word;
  392.     Len: Word;
  393. Begin
  394. FillChar(TxtRec,SizeOf(TxtRec),#00);
  395. If ObjRec.OFail.Length<>0
  396.    Then Begin
  397.         Seek(TxtFile,ObjRec.OFail.Start);
  398.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.OFail.Length),RR);
  399.         End
  400.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  401. End;
  402.  
  403. (*---------------------------------------------------------------------------*
  404.   Read the OSUCCESS tekst of the current record
  405.  *---------------------------------------------------------------------------*)
  406. Procedure Database.OSuccess(Msg : String);
  407. Var RR : Word;
  408.     Cnt: Word;
  409.     Len: Word;
  410. Begin
  411. FillChar(TxtRec,SizeOf(TxtRec),#00);
  412. If ObjRec.OSuccess.Length<>0
  413.    Then Begin
  414.         Seek(TxtFile,ObjRec.OSuccess.Start);
  415.         BlockRead(TxtFile,TxtRec,MaxLen(ObjRec.OSuccess.Length),RR);
  416.         End
  417.    Else Move(Msg[1],TxtRec[0],Length(Msg));
  418. End;
  419.  
  420. (*---------------------------------------------------------------------------*
  421.   Return the name of the current object
  422.  *---------------------------------------------------------------------------*)
  423. Function Database.Name:String;
  424. Begin
  425. If Pos(';',ObjRec.Name)>0
  426.    Then Name:=Copy(ObjRec.Name,1,Pos(';',ObjRec.Name)-1)
  427.    Else Name:=ObjRec.Name;
  428. End;
  429.  
  430. (*---------------------------------------------------------------------------*
  431.   Functions to check the used flags.
  432.  *---------------------------------------------------------------------------*)
  433. Function Database.IsRoom:Boolean;
  434. Begin
  435. IsRoom:=ObjRec.ObjType = Room_Type;
  436. End;
  437.  
  438. Function Database.IsThing:Boolean;
  439. Begin
  440. IsThing:=ObjRec.ObjType = Thing_Type;
  441. End;
  442.  
  443. Function Database.IsExit:Boolean;
  444. Begin
  445. IsExit:=ObjRec.ObjType = Exit_Type;
  446. End;
  447.  
  448. Function Database.IsPlayer:Boolean;
  449. Begin
  450. IsPlayer:=ObjRec.ObjType = Player_Type;
  451. End;
  452.  
  453. Function Database.IsDrone:Boolean;
  454. Begin
  455. IsDrone:=ObjRec.ObjType = DRONE_Type;
  456. End;
  457.  
  458.  
  459. Function Database.LevelOk(Level : Byte):Boolean;
  460. Begin
  461. LevelOk:=ObjRec.ObjLevel>=Level;
  462. End;
  463.  
  464.  
  465. Function DataBase.IsLinkOk:Boolean;
  466. Begin
  467. IsLinkOk:=(ObjRec.Attr_Flags And Link_Ok_Flag)=Link_Ok_Flag;
  468. End;
  469.  
  470. Function Database.IsSticky:Boolean;
  471. Begin
  472. IsSticky:=(ObjRec.Attr_Flags And Sticky_Flag) = Sticky_Flag;
  473. End;
  474.  
  475. Function Database.IsInvisible:Boolean;
  476. Begin
  477. IsInvisible:=(ObjRec.Attr_Flags And InVisible_Flag) = InVisible_Flag;
  478. End;
  479.  
  480. Function DataBase.IsForSale:Boolean;
  481. Begin
  482. IsForSale:=(ObjRec.Attr_Flags And For_Sale_Flag)=For_Sale_Flag;
  483. End;
  484.  
  485. Function DataBase.IsChownOK:Boolean;
  486. Begin
  487. IsChownOK:=(ObjRec.Attr_Flags And Chown_ok_Flag)=Chown_ok_Flag;
  488. End;
  489.  
  490.  
  491. Function Database.IsTemple:Boolean;
  492. Begin
  493. IsTemple:=(ObjRec.Room_Flags And Temple_Room)=Temple_Room;
  494. End;
  495.  
  496. Function Database.IsHaven:Boolean;
  497. Begin
  498. IsHaven:=(ObjRec.Room_Flags And Haven_Room)=Haven_Room;
  499. End;
  500.  
  501. Function Database.IsShop:Boolean;
  502. Begin
  503. IsShop:=(ObjRec.Room_Flags And Shop_Room)=Shop_Room;
  504. End;
  505.  
  506. Function Database.IsLoud:Boolean;
  507. Begin
  508. IsLoud:=(ObjRec.Room_Flags And Loud_Room)=Loud_Room;
  509. End;
  510.  
  511. Function Database.CanTeleport:Boolean;
  512. Begin
  513. CanTeleport:=(ObjRec.Attr_Flags And Teleport_Ok_Flag)=Teleport_Ok_Flag;
  514. End;
  515.  
  516.  
  517.  
  518. Function Database.IsOwnedBy(Player : Integer):Boolean;
  519. Begin
  520. IsOwnedBy:=ObjRec.Owner=Player;
  521. End;
  522.  
  523. Function DataBase.IsOwner(ObjNr : Integer):Boolean;
  524. Begin
  525. IsOwner:=ObjRec.Owner=ObjNr;
  526. End;
  527.  
  528.  
  529. Function Database.WhichGender:GenderType;
  530. Begin
  531. WhichGender:=GenderType(ObjRec.Sex);
  532. End;
  533.  
  534. End.
  535.